package com.example.lenovo.demo.presenter.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.lenovo.demo.R;
import com.example.lenovo.demo.StrategyChildActivity;
import com.example.lenovo.demo.model.databean.StrategyBean;
import com.example.lenovo.demo.view.utils.GvHeightUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 0:概览
 * 1:出行通知
 * 2:如何到达
 * 3:当地交通
 * 4:景点
 * 5:娱乐
 * 6:住宿
 * 7:美食
 * 8:购物
 * 9:离境须知
 * 10:其他
 */

public class StrategyAcLvAdapter extends BaseAdapter implements AdapterView.OnItemClickListener{
    private List<StrategyBean> data;
    private Context context;
    private String[] names;
    private String[] names_en;
    private int[] imgs={R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four,R.drawable.five,R.drawable.six,
                        R.drawable.seven,R.drawable.eight,R.drawable.nine,R.drawable.ten,R.drawable.eleven,R.drawable.other};
    public StrategyAcLvAdapter(List<StrategyBean> data, Context context) {
        this.data = data;
        this.context = context;
        this.names=context.getResources().getStringArray(R.array.names);
        this.names_en=context.getResources().getStringArray(R.array.names_en);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if(view==null){
            viewHolder=new ViewHolder();
            view= LayoutInflater.from(context).inflate(R.layout.strategy_lv_item,null,false);
            viewHolder.name= (TextView) view.findViewById(R.id.strategyAc_Lv_item_name);
            viewHolder.name_en= (TextView) view.findViewById(R.id.strategyAc_Lv_item_name_en);
            viewHolder.gridView= (GridView) view.findViewById(R.id.strategyAc_Lv_item_gv);
            viewHolder.imageView= (ImageView) view.findViewById(R.id.strategyAc_Lv_item_img);
            view.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) view.getTag();
        }
        StrategyBean strategyBean=data.get(i);
        viewHolder.name.setText(names[strategyBean.getCategory_type()]);
        viewHolder.name_en.setText(names_en[strategyBean.getCategory_type()]);
        viewHolder.imageView.setImageResource(imgs[strategyBean.getCategory_type()]);
        StrategyAcLvGvAdapter adapter=new StrategyAcLvGvAdapter(data.get(i).getPages(),context);
        viewHolder.gridView.setAdapter(adapter);
        viewHolder.gridView.setTag(i);
        GvHeightUtil.setGirdViewHeightBasedOnChildren(viewHolder.gridView,2);
        viewHolder.gridView.setOnItemClickListener(this);


        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        int totalnum=0;
        int num= (int) adapterView.getTag();
        for (int a = 0; a < num; a++) {
            totalnum+=data.get(a).getPages().size();
        }
        totalnum+=i;
        Intent intent=new Intent(context, StrategyChildActivity.class);
        Bundle bundle=new Bundle();
        bundle.putParcelableArrayList("msg", (ArrayList<? extends Parcelable>) data);
        bundle.putInt("numparent",num);
        bundle.putInt("numchild",i);
        bundle.putInt("totalnum",totalnum);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    class ViewHolder{
        TextView name,name_en;
        ImageView imageView;
        GridView gridView;
    }
}
